package com.example.lanouhn.bantangapp.activities.adapters;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.contants.Search_product;

import java.util.List;

/**
 * Created by lanouhn on 16/7/30.
 */
public class Adapter_FragmentSearchListleft extends BaseAdapter {
    private List<Search_product> list;
    private Context context;
    private TextView text;
    private View     left_line;//左边的线
    int selectedPos = 0;//定义一个标示
    public Adapter_FragmentSearchListleft(List<Search_product> list, Context context) {
        this.list = list;
        this.context = context;
    }


    //   自定义一个方法，使标示等于position，通知系统刷新
    public void setSelectPosition(int position) {
        selectedPos = position;
        notifyDataSetChanged();
    }

    //自定义一个方法  把标示返回给Activity中
    public int setSelectPosition() {
        return selectedPos;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            View view= LayoutInflater.from(context).inflate(R.layout.fragment_search_itemleft,null);
            text= (TextView) view.findViewById(R.id.text);
            left_line=view.findViewById(R.id.left_line);
        Search_product esse = list.get(position);
            //判断是否点击itme ，点击是给他设置不同的颜色和背景
            if (selectedPos == position) {
                text.setBackgroundColor(Color.WHITE);
                view.setBackgroundColor(Color.WHITE);
                text.setTextColor(Color.RED);
                left_line.setVisibility(View.VISIBLE);
            } else {
                view.setBackgroundColor(context.getResources().getColor(R.color.gray));
                text.setTextColor(Color.BLACK);
            }
            //给Textview赋值
            text.setText(esse.getName());

        return view;
    }

}
